package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\20 0020 18:39
 * @class describe
 */
public class RenovatePersonInfoActivity extends BaseActivity {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_save)
    public TextView tvSave;
    @BindView(R.id.ed_name)
    public EditText edName;
    @BindView(R.id.img_clear)
    public ImageView imgClear;
    @BindView(R.id.rl_name)
    public RelativeLayout rlName;
    @BindView(R.id.rl_send_word)
    public RelativeLayout rlSendWord;
    @BindView(R.id.ed_address)
    public EditText edAddress;
    @BindView(R.id.rl_address)
    public RelativeLayout rlAddress;
    @BindView(R.id.ed_send_word)
    public EditText edSendWord;
    @BindView(R.id.tv_send_word)
    public TextView tvSendWord;

    private int type = -1;//区分界界面显示是修改姓名、寄语、家庭住址
    private String content;//传过来要修改的内容

    public static Intent newIntent(Activity activity, int type, String content) {
        Intent intent = new Intent(activity, RenovatePersonInfoActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("content", content);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renovate_person_info);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        type = getIntent().getIntExtra("type", 1);
        content = getIntent().getStringExtra("content");
        rlTitle.setPadding(0, this.height, 0, 0);

        if (type == 1) {
            tvTitle.setText("修改姓名");
            edName.setText(content);
            rlName.setVisibility(View.VISIBLE);
            rlSendWord.setVisibility(View.GONE);
            rlAddress.setVisibility(View.GONE);
        }
        if (type == 2) {
            tvTitle.setText("修改寄语");
            edSendWord.setText(content);
            rlName.setVisibility(View.GONE);
            rlSendWord.setVisibility(View.VISIBLE);
            rlAddress.setVisibility(View.GONE);
        }
        if (type == 3) {
            tvTitle.setText("修改家庭住址");
            edAddress.setText(content);
            rlName.setVisibility(View.GONE);
            rlSendWord.setVisibility(View.GONE);
            rlAddress.setVisibility(View.VISIBLE);
        }

        edName.setSelection(edName.getText().toString().length());
        edAddress.setSelection(edAddress.getText().toString().length());
        edSendWord.setSelection(edSendWord.getText().toString().length());
        tvSendWord.setText((18 - edSendWord.getText().toString().length()) + "");

        edSendWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.toString().length();
                length = 18 - length;
                tvSendWord.setText(length + "");
            }
        });

    }

    @OnClick({R.id.img_back, R.id.img_clear, R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.img_clear:
                edName.setText("");
                break;
            case R.id.tv_save:
                Intent intent = new Intent();
                if (type == 1) {
                    intent.putExtra("name", edName.getText().toString());
                }
                if (type == 2) {
                    intent.putExtra("sendword", edSendWord.getText().toString());
                }
                if (type == 3) {
                    intent.putExtra("address", edAddress.getText().toString());
                }
                setResult(type, intent);
                this.finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}